package com.chuanjiabao.wisdom.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.databinding.DataBindingUtil;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.chuanjiabao.wisdom.R;
import com.chuanjiabao.wisdom.bean.FamilyDeatilCallbackBean;
import com.chuanjiabao.wisdom.databinding.ItemFamilyNameListBinding;
import com.chuanjiabao.wisdom.listener.OnItemClickListener;
import com.chuanjiabao.wisdom.listener.OnPositionClickListener;
import com.chuanjiabao.wisdom.util.GlideUtil;

import java.util.List;

public class FamilyNameListAdapter extends RecyclerView.Adapter<BaseRecyclerViewHolder> {
    private List<FamilyDeatilCallbackBean.DataBean.MembersBean> strings;
    private Context mcontext;
    OnItemClickListener mOnItemClickLitener;
    OnPositionClickListener onPositionClickListener;

    public void setOnPositionClickListener(OnPositionClickListener listener){
        this.onPositionClickListener = listener;
    }

    public FamilyNameListAdapter(Context context, List<FamilyDeatilCallbackBean.DataBean.MembersBean> strings) {
        this.mcontext = context;
        this.strings = strings;
    }

    @Override
    public BaseRecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        ItemFamilyNameListBinding binding = DataBindingUtil.inflate(
                LayoutInflater.from(mcontext),
                R.layout.item_family_name_list,
                parent,
                false
        );
        binding.setAdapter(this);
        return new TourViewHolder(binding);
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(final BaseRecyclerViewHolder holder, final int position) {
        holder.bindData(strings.get(position));
        final TourViewHolder tv = (TourViewHolder) holder;

        if (TextUtils.isEmpty(strings.get(position).getD_head_pic())) {
            GlideUtil.setImgMethod(R.mipmap.icon_default_user, tv.ivModule);
        }else{
            GlideUtil.setCircleMethod(mcontext,strings.get(position).getD_head_pic(),tv.ivModule);
        }

        tv.tvModuleSelect.setOnClickListener(v -> {
            if (onPositionClickListener != null) {
                onPositionClickListener.setOnPositionClickListener(position);
            }
        });

        tv.tvModuleName.setText(strings.get(position).getD_name());

        if (mOnItemClickLitener != null) {
            holder.itemView.setOnClickListener(v -> {
                int pos = holder.getLayoutPosition();
                mOnItemClickLitener.onItemClick(holder.itemView, pos);
            });

            holder.itemView.setOnLongClickListener(v -> {
                int pos = holder.getLayoutPosition();
                mOnItemClickLitener.onItemLongClick(holder.itemView, pos);
                return false;
            });
        }
    }

    public void setOnItemClickLitener(OnItemClickListener mOnItemClickLitener) {
        this.mOnItemClickLitener = mOnItemClickLitener;
    }

    @Override
    public int getItemCount() {
        return strings.size();
    }

    public class TourViewHolder extends BaseRecyclerViewHolder<FamilyDeatilCallbackBean.DataBean.MembersBean> {
        ItemFamilyNameListBinding functionBinding;
        ImageView ivModule;
        TextView tvModuleName;
        CheckBox tvModuleSelect;

        public TourViewHolder(ItemFamilyNameListBinding dataBinding) {
            super(dataBinding.getRoot());
            functionBinding = dataBinding;
            ivModule = dataBinding.ivModule;
            tvModuleName = dataBinding.tvModuleName;
            tvModuleSelect = dataBinding.tvModuleSelect;
        }

        @Override
        public void bindData(FamilyDeatilCallbackBean.DataBean.MembersBean data) {
            functionBinding.setBean(data);
        }
    }

    @Override
    public int getItemViewType(int position) {
        return strings.size();
    }

}